import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const AdminLayout = () => import('@/layouts/AdminLayout.vue')
const VideoManagement = () => import('@/views/VideoManagement.vue')
const LiveManagement = () => import('@/views/LiveManagement.vue')
const LoginView = () => import('@/views/LoginView.vue')

const children: RouteRecordRaw[] = [
  { path: 'dashboard', name: 'dashboard', component: () => import('@/views/DashboardView.vue') },
  { path: 'admin-referral', name: 'admin-referral', component: () => import('@/views/AdminReferralManagement.vue') },
  { path: 'notice', name: 'notice', component: () => import('@/views/NoticeFaqManagement.vue') },
  { path: 'advertisement-management', name: 'advertisement-management', component: () => import('@/views/AdvertisementManagement.vue') },
  { path: 'payment', name: 'payment', component: () => import('@/views/PaymentManagement.vue') },
  { path: 'point-package-management', name: 'point-package-management', component: () => import('@/views/PointPackageManagement.vue') },
  { path: 'gift-sends', name: 'gift-sends', component: () => import('@/views/GiftSendRecords.vue') },
  { path: 'gift-send-management', name: 'gift-send-management', component: () => import('@/views/GiftSendManagement.vue') },
  { path: 'gift-management', name: 'gift-management', component: () => import('@/views/GiftManagement.vue') },
  { path: 'members', name: 'members', component: () => import('@/views/UserManagement.vue') },
  { path: 'partners', name: 'partners', component: () => import('@/views/PartnerManagement.vue') },
  { path: 'channel-management', name: 'channel-management', component: () => import('@/views/ChannelManagement.vue') },
  { path: 'live-management', name: 'live-management', component: LiveManagement },
  { path: 'chat-rooms', name: 'chat-rooms', component: () => import('@/views/ChatRoomsManagement.vue') },
  { path: 'chat-rooms/:id/view', name: 'admin-chat-room-view', component: () => import('@/views/AdminChatRoomView.vue') },
  { path: 'video-management', name: 'video-management', component: VideoManagement },
  { path: 'ranking-management', name: 'ranking-management', component: () => import('@/views/RankingManagement.vue') },
  { path: 'logs', name: 'logs', component: () => import('@/views/UserLogsView.vue') },
]

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
  },
  {
    path: '/',
    component: AdminLayout,
    children: [
      { path: '', redirect: '/dashboard' },
      ...children,
    ],
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()

  // 如果访问登录页面，且已经登录，则重定向到仪表盘
  if (to.path === '/login') {
    if (authStore.isLoggedIn) {
      const isValid = await authStore.checkAuth()
      if (isValid) {
        next('/dashboard')
        return
      }
    }
    next()
    return
  }

  // 其他页面需要登录
  if (!authStore.isLoggedIn) {
    next('/login')
    return
  }

  // 检查认证状态
  const isValid = await authStore.checkAuth()
  if (!isValid) {
    next('/login')
    return
  }

  // 合作伙伴权限检查
  if (authStore.isPartner) {
    const allowedPaths = ['/dashboard', '/chat-rooms', '/gift-send-management', '/members']
    const allowedPathPatterns = [/^\/chat-rooms\/\d+\/view$/] // 允许访问聊天室界面

    const isAllowed = allowedPaths.includes(to.path) ||
                     allowedPathPatterns.some(pattern => pattern.test(to.path))

    if (!isAllowed) {
      // 如果访问不允许的页面，重定向到仪表盘
      next('/dashboard')
      return
    }
  }

  next()
})

export default router
